<template>
  <div>
    <d2-crud
      ref="d2Crud"
      :columns="columns"
      :data="dataList"
      :options="options"
      @more-model="showDetail"
      :index-row="{title:'序号'}"
      selection-row
      :rowHandle="rowHandle"
      :pagination="pagination"
      @row-click="rowClick"
      @selection-change="selectChange"
      @pagination-current-change="pageChange">
      <div class="title" slot="header">
        <span class="text">操作日志</span>
        <!-- <head-btns :add="add" :mod="mod" :del="del"/> -->
        <div class="dateFilter">
          <span>
            <el-date-picker
              v-model="date"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </span>
          <span style="margin-left:20px;"><el-button type="primary" @click="search"> 搜索</el-button></span>
        </div>
      </div>
    </d2-crud>
    <detail-info v-model="detailVisible" ref="detailFrom" @submit="_fetchListData"/>
  </div>
</template>
<script>
  import detailInfo from "./detail-info";
  import workflowApi from '@/api/system/workflow';
  import zhzfUtils from '@/assets/utils/zhzf-util';
  import dictCache from '@/assets/utils/cache-dict-util';
  import HeadBtns from "@/views/comps/zhzf/head-btns";
  import {mapState, mapActions} from 'vuex'

  export default {
    components: {HeadBtns, detailInfo},
    data() {
      return {
        dataList: [],
        detailVisible: false,
        columns: [
          {
            title: '类型', key: "actionName", width: 50
          },
          {title: '操作人', key: 'userName', width: 100},
          {title: 'IP', key: 'operIp', width: 180},
          {title: 'URL', key: 'operUrl', width: 200,},
          {
            title: '操作时间', key: 'operationDatetime', formatter: (row, col, val) => {
              return zhzfUtils.formatDts(val, 'YYYY-MM-DD HH:mm:ss')
            }
          },
          {title: '模块', key: 'moduleTitle'},
        ],
        rowHandle: {
          custom: [
            {text: '详情', size: 'mini', emit: 'more-model', style:'color:#027BE3'},
          ]
        },
        options: {
          stripe: true,
          height: zhzfUtils.getPageheight(),
          highlightCurrentRow: true
        },
        pagination: {
          currentPage: 1,
          pageSize: zhzfUtils.getPageSize(),
          total: 0
        },
        queryParam: {},
        selected: [],
        date: []
      };
    },
    computed: {
      ...mapState('d2admin/user', [
        'info'
      ])
    },
    mounted() {
      this.initPage();
    },
    methods: {
      async initPage() {
        await this._fetchListData();
      },
      async _fetchListData(date) {
        let resp = null;
        if (date) {
          resp = await workflowApi.logOperation({...this.pagination, userId: this.info.id, begin: date[0], end: date[1]});
        } else {
          resp = await workflowApi.logOperation({...this.pagination, userId: this.info.id});
        }
        this.dataList = resp.data.rows;
        this.pagination.total = this.dataList.length;
      },
      pageChange(currentPage) {
        this.pagination.currentPage = currentPage;
        this._fetchListData();
      },
      rowClick(row) {
        this.$refs.d2Crud.$children[2].toggleRowSelection(row);
      },
      selectChange(selection) {
        this.selected = selection;
      },
      showDetail({row}) {
        window.event.stopPropagation(); //阻止冒泡
        this.detailVisible = true;
        this.$refs.detailFrom.initPage(row);
      },
      async search() {
        let date = [];
        for (let i = 0; i < this.date.length; i++) {
          date.push(zhzfUtils.formatDts(this.date[i], 'YYYY-MM-DD'));
        }
        await this._fetchListData(date);
      },
      dataTransform(val) {
        switch (val) {
          case '0':
            return "其他";
            break;
          case '1':
            return '新增';
            break;
          case '2':
            return '修改';
            break;
          case '3':
            return '保存';
            break;
          case '4':
            return '删除';
            break;
          case '5':
            return '授权';
            break;
          case '6':
            return '导出';
            break;
          case '7':
            return '导入';
            break;
          case '8':
            return '强退';
            break;
          case '9':
            return '禁止访问';
            break;
          case '10':
            return '生成代码';
            break;
        }
      },
      add(rd) {
        this.showDetail();
      },
      mod(rd) {
        if (rd) {
          this.showDetail(rd.row);
        } else if (this.selected.length == 1) {
          this.showDetail(this.selected[0]);
        } else {
          utils.showTips('请选择一行数据');
          return;
        }
      },
      del(rd) {
        if (!rd && !this.selected.length) {
          utils.showTips('请选择要删除的数据', false);
          return;
        }
        let delIds = [];
        if (rd) {
          delIds = [rd.row.id];
        } else {
          this.selected.map(item => {
            delIds.push(item.id)
          });
        }

        this.$confirm("确认删除？", "确认信息", {
          distinguishCancelAndClose: true,
          confirmButtonText: "删除",
          cancelButtonText: "取消"
        }).then(() => {
          this.submitDel(delIds)
        });
      },
      async submitDel(ids) {
        await discretionApi.del(ids);
        await this.initPage();
      }
    }
  };
</script>

<style lang="scss">
  .dateFilter {
    float: right;
    margin-right: 30px;
  }
</style>
